﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectClass.DAL;
using ProjectClass.ShopingCart;
namespace ProjectClass.View.FrontEnd
{
    public partial class Detail : System.Web.UI.Page
    {
        ProjectClassEntities db = new ProjectClassEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["ProductID"] != null)
                ProductID = int.Parse(Request["ProductID"]);
            else
                Response.Redirect("Home.aspx");
            if (!Page.IsPostBack)
            {
                LoadDetail();
                LoadGallery();
                LoadRelateProduct();
            }
        }
        private int ProductID { get; set; }
        private int CategoryID { get; set; }
        private void LoadDetail()
        {
            var product = db.tbl_Product.Include("tbl_ProductGallery").Include("tbl_ProductDetail")
                .Single(m=>m.ProductId==ProductID);
            CategoryID = product.CategoryID;
            Category.Text = product.tbl_Category.Name;
            productName.Text = product.Name;
            ShortTech.Text = product.ShortTechInfo;
            ShortDescript.Text = product.ShortDescription;
            FullDescript.Text = product.FullDescription;
            ProColor.Text = product.tbl_ProductDetail.Single().Color;
            ProCPU.Text = product.tbl_ProductDetail.Single().CPU;
            ProCamera.Text = product.tbl_ProductDetail.Single().MainCam;
            ProWeight.Text = product.tbl_ProductDetail.Single().Weight.Value.ToString()+" gr";
            ProWarranty.Text = product.tbl_ProductDetail.Single().Warantty;
            ProSize.Text = product.tbl_ProductDetail.Single().Size;
            ProBattery.Text = product.tbl_ProductDetail.Single().Battery;
            ProGPS.Text = product.tbl_ProductDetail.Single().GPS;
            ProMemory.Text = product.tbl_ProductDetail.Single().MemorySupport;
            ProInternalMemory.Text = product.tbl_ProductDetail.Single().InternalMemory;
            ProScreen.Text = product.tbl_ProductDetail.Single().ScreenSize;
            ProOS.Text = product.tbl_ProductDetail.Single().OS;
            ProType.Text = product.tbl_ProductDetail.Single().ProductType;
            Pro3G.Text = product.tbl_ProductDetail.Single().ThreeG;
            ProWLAN.Text = product.tbl_ProductDetail.Single().WLAN;
            productPrice.Text = string.Format("{0:0,0}", product.Price) + "đ";
            productAvatar.ImageUrl = "/Content/ProductImg/" + product.Avatar;
        }
        private void LoadGallery()
        {
            reGallery.DataSource = db.tbl_ProductGallery.Where(m => m.ProductId == ProductID).ToList();
            reGallery.DataBind();
        }

        protected void btnAddCart_Click(object sender, EventArgs e)
        {
            ShopingCart.ShoppingCart.Instance.AddItem(ProductID);
            ShoppingCart.Instance.SetItemQuantity(ProductID,int.Parse(txtquantity.Text));
            Response.Redirect("CheckoutStepOne.aspx");
        }

        private void LoadRelateProduct()
        {
            //int CategoryId = db.tbl_Product.Single(m => m.ProductId == ProductID).CategoryID;
            rprelateproduct.DataSource = db.tbl_Product.Where(m => m.ProductId != ProductID && m.CategoryID == CategoryID).ToList();
            rprelateproduct.DataBind();
        }
    }
}